//
//  AddressCityViewController.m
//  Youhuiju
//
//  Created by liuovyue on 16/9/8.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "AddressCityViewController.h"
#import "CityPicker.h"

@interface AddressCityViewController ()<UIPickerViewDelegate ,UIPickerViewDataSource>
@property (nonatomic, strong) UILabel *cityLabel;
@property (nonatomic, strong) UIPickerView *cityPicker;
@property (nonatomic, strong) UIView *viewAddressBackground;
@property (nonatomic, strong) UIButton *buttonRemove;
@property (nonatomic, strong)NSMutableString *str;

@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonSure;
@property (nonatomic, strong)UIView *viewBack;
@end

@implementation AddressCityViewController

{
    NSInteger row1;
    NSInteger row2;
    NSInteger row3;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self createNavigationBar];
    self.view.backgroundColor = [UIColor whiteColor];
    self.cityLabel = [[UILabel alloc]init];
    [self.view addSubview:self.cityLabel];
    
    [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.viewBar.mas_bottom).offset(100*FitH);
        
    }];
    
    
                self.viewAddressBackground = [[UIView alloc]init];
    
    
                self.viewAddressBackground.backgroundColor = [UIColor colorWithRed:0.6424 green:0.6424 blue:0.6424 alpha:0.0];
    
    
                [self.view addSubview:self.viewAddressBackground];
                [self.viewAddressBackground mas_makeConstraints:^(MASConstraintMaker *make) {
    
                    make.top.equalTo(self.viewBar.mas_bottom);
                    make.left.equalTo(self.view.mas_left);
                    make.right.equalTo(self.view.mas_right);
                    make.bottom.equalTo(self.view.mas_bottom);
    
    
                }];
    
    
                UIButton *buttonRemove = [UIButton buttonWithType:UIButtonTypeSystem];
                //[buttonRemove addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
    
                [self.viewAddressBackground addSubview:buttonRemove];
                [buttonRemove mas_makeConstraints:^(MASConstraintMaker *make) {
    
                    make.top.equalTo(self.view.mas_top);
                    make.left.equalTo(self.view.mas_left);
                    make.right.equalTo(self.view.mas_right);
                    make.bottom.equalTo(self.view.mas_bottom);
    
    
                }];
    
    
    
                UIView *viewAddressTwoBackground = [[UIView alloc]init];
                viewAddressTwoBackground.backgroundColor = [UIColor whiteColor];
                [buttonRemove addSubview:viewAddressTwoBackground];
    
                [viewAddressTwoBackground mas_makeConstraints:^(MASConstraintMaker *make) {
    
                    make.height.mas_equalTo(300*FitH);
                    make.left.equalTo(self.view.mas_left);
                    make.right.equalTo(self.view.mas_right);
                    make.bottom.equalTo(self.view.mas_bottom);
    
    
                }];
    
    
                row1 = 0;
                row2 = 0;
                row3 = 0;
    
                self.cityPicker = [[UIPickerView alloc]init];
                //self.cityPicker.tag = 10;
                self.cityPicker.delegate = self;
                self.cityPicker.dataSource = self;
                self.cityPicker.showsSelectionIndicator = YES;
                [viewAddressTwoBackground addSubview:self.cityPicker];
                self.cityPicker.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
                [self.cityPicker mas_makeConstraints:^(MASConstraintMaker *make) {
    
                    make.height.mas_equalTo(250*FitH);
                    make.left.equalTo(self.view.mas_left);
                    make.right.equalTo(self.view.mas_right);
                    make.bottom.equalTo(self.view.mas_bottom);
    
    
                }];
    
    
                UIButton *buttonSure = [UIButton buttonWithType:UIButtonTypeSystem];
                buttonSure.tintColor = [UIColor blackColor];
                [buttonSure setTitle:@"确定" forState: UIControlStateNormal];
                buttonSure.layer.borderColor = [UIColor grayColor].CGColor;
                [buttonSure.layer setMasksToBounds:YES];
                [buttonSure.layer setCornerRadius:16.0];
                [buttonSure.layer setBorderWidth:1.0];
                [buttonSure addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
                [viewAddressTwoBackground addSubview:buttonSure];
    
                [buttonSure mas_makeConstraints:^(MASConstraintMaker *make) {
    
                    make.top.equalTo(viewAddressTwoBackground.mas_top).offset(10*FitH);
                    make.right.equalTo(self.viewAddressBackground.mas_right).offset(-10*FitW);
                    make.width.mas_equalTo(80*FitW);
                    make.height.mas_equalTo(30*FitH);
                    
                }];

    
    
}

#pragma mark - 添加顶部信息显示
- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"选择收货地址";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    
    
    
}


- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)createData {
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(data:) name:@"address" object:nil];
    
    
    
}

- (void)data:(NSDictionary *)dic {
    
    NSLog(@"dic");
}

#pragma mark -- UIPickerView 代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (pickerView == self.cityPicker) {
        return 3;
        
    }else {
        return  1;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    
    if (component == 0) {
        return [CityPicker getCityData].count;
    }
    else if(component == 1) {
        
        NSArray *array = [CityPicker getCityData][row1][@"children"];
        if ((NSNull *)array != [NSNull null]) {
            return array.count;
        }
        
        return 0;
    }else {
        NSArray *array = [CityPicker getCityData][row1][@"children"];
        if ((NSNull *)array != [NSNull null]) {
            NSArray *array1 = [CityPicker getCityData][row1][@"children"][row2][@"children"];
            if ((NSNull*)array1 != [NSNull null]) {
                
                return array1.count;
                
            }
            return 0;
            
            
        }
        
        return 0;
        
    }
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    if (component == 0) {
        return [CityPicker getCityData][row][@"name"];
        
    }else if (component == 1){
        return [CityPicker getCityData][row1][@"children"][row][@"name"];
    }else if(component == 3){
        return [CityPicker getCityData][row1][@"children"][row2][@"children"][row][@"name"];
    }
    return nil;
    
    
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        row1 = row;
        row2 = 0;
        row3 = 0;
        
        [self.cityPicker reloadComponent:1];
        [self.cityPicker reloadComponent:2];
        
        
    }else if (component == 1) {
        row2 = row;
        row3 = 0;
        [self.cityPicker reloadComponent:2];
    }
    else {
        row3 = row;
    }
    
    NSInteger cityRow1 = [self.cityPicker selectedRowInComponent:0];
    NSInteger cityRow2 = [self.cityPicker selectedRowInComponent:1];
    NSInteger cityRow3 = [self.cityPicker selectedRowInComponent:2];
    self.str = [[NSMutableString alloc]init];
    [self.str appendString:[CityPicker getCityData][cityRow1][@"name"]];
    NSArray *array = [CityPicker getCityData][cityRow1][@"children"];
    if ((NSNull *)array != [NSNull null]) {
        [self.str appendString:[CityPicker getCityData][cityRow1][@"children"][cityRow2][@"name"]];
        NSArray *array1 = [CityPicker getCityData][cityRow1][@"children"][cityRow2][@"children"];
        if ((NSNull *)array1 != [NSNull null]) {
            [self.str appendString:[CityPicker getCityData][cityRow1][@"children"][cityRow2][@"children"][cityRow3][@"name"]];
        }
        
        
    }
    
    self.cityLabel.text = self.str;
   
   
    
    
    
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 107, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:14*FitW];
    
    titleLabel.backgroundColor = [UIColor clearColor];
    if (component ==0 ) {
        
        titleLabel.text = [CityPicker getCityData][row][@"name"];
    }else if (component == 1){
        titleLabel.text = [CityPicker getCityData][row1][@"children"][row][@"name"];
    }else {
        titleLabel.text = [CityPicker getCityData][row1][@"children"][row2][@"children"][row][@"name"];
    }
    
    return titleLabel;
    
}

- (void)sureAction:(UIButton *)button {
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
    [self.delegate passAddress:self.cityLabel];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
